﻿<#assign ctx = request.contextPath />
<#macro paint name>
  <#assign grid = request.getAttribute(name) />
  <div class="class_box">
	<div class="lei_box3 plan">
      <table id="${name}" pagination="${name}_pagination" <#if grid.filter?exists> filter="${grid.filter}" </#if> ></table>
    </div>
    <div class="divline"></div>
  <div id='${name}_pagination'>
    <#include "/common/pagination.ftl"/>
  </div>
</div>
<script>
init_${name} = function(){
    var lastIndex;
	var _grid = $('#${name}');
	_grid.datagrid({
		  width:<#if grid.width?ends_with("%")>'${grid.width}'<#else>${grid.width}</#if>,
		  singleSelect:false,
		  border:true,
		  striped: true,//行与行之间颜色区分
		  url:'${grid.url!""}',
		  queryParams:{},
		  loadMsg:'正在加载数据,请等待...',
		  idField:'shopOrderID',
		  columns:[[
		  <#list grid.columns as col>
		    <#if col_index gt 0>,</#if>
            {field:'${col.name}',title:'${col.title}',width:${col.width}
              <#if col.sortable>,sortable:true</#if>
              <#if col.checkbox>,checkbox:true</#if>
              <#if col.editorType == Const.EDITOR_NUMBER>,editor:{type:'number'}
              <#elseif col.editorType == Const.EDITOR_TEXT>,editor:'text'
              <#elseif col.editorType == Const.EDITOR_SELECT>
                ,editor:{
							type:'combobox',
							options:{
								valueField:'id',
								textField:'text',
								data:${col.optionData}
							}
						}
              </#if>
              <#if col.formatter?exists>
                ,formatter:function(value,row){ ${col.formatter} } 
              </#if>
            }	            
          </#list>  
	      ]],
		  onLoadSuccess: function(data){
		      if($(this).attr('pagination')){
		    	  var dom_pagination = $('#'+$(this).attr('pagination')+'');
		    	  var total = data.total;
		    	  dom_pagination.pagination(this,total); 
		      }
	      },

	      onClickRow:function(rowIndex,rowData){
			clickedRow = rowData;
			if (lastIndex != rowIndex){
				$('#${name}').datagrid('endEdit', lastIndex);
				$('#${name}').datagrid('beginEdit', rowIndex);
			}
			lastIndex = rowIndex;
		  },

		  onSelect:function(rowIndex,rowData){
		    clickedRow = rowData;
            if(lastIndex && lastIndex != rowIndex){
               _grid.datagrid('endEdit',lastIndex);
            }
            lastIndex = rowIndex;
            _grid.datagrid('beginEdit',rowIndex);
            var et = util.getEventTarget();
            if($('input',et).size()>0){
               $('input',et).get(0).focus();
             } 
             var editors = _grid.datagrid('getEditors',rowIndex);
             $.each(editors,function(i,editor){
                $(editor.target).bind('blur',function(){
                   setTimeout(function(){
                     var focusElement = document.activeElement;
                     if($(focusElement).parents('.datagrid').size()== 0){
                        _grid.datagrid('endEdit',rowIndex);
                     }                    
                     },0)                
                }).bind('focus',function(){
                 _grid.datagrid('selectRow',rowIndex);
             });
             })            
          },
          onAfterEdit:function(index,rowData){
	          if(rowData.logisticsSendNum<0){
		          alert("编号:"+rowData['shopOrderCode']+",配送量不能小于零");
		          $('#${name}').datagrid('beginEdit', index);
	          }else if(rowData.logisticsSendNum>rowData.logisticsAssignNum){
	          	  alert("编号:"+rowData['shopOrderCode']+",配送量不能大于分配量");
	          	  $('#${name}').datagrid('beginEdit', index);
	          }
	          <!-- 门店收订货、交退货 -->
	          if(rowData.shopOrderType == 0){
		          if(rowData.shopConfirmNum<0){
		          	  alert("编号:"+rowData['shopOrderCode']+",收货量不能小于零");
		          	  $('#${name}').datagrid('beginEdit', index);
		          }else if(rowData.shopConfirmNum>rowData.logisticsSendNum){
		              alert("编号:"+rowData['shopOrderCode']+",收货量不能大于实际配送量");
		          	  $('#${name}').datagrid('beginEdit', index);
		          }          
	          }else{
		          if(rowData.shopConfirmNum<0){
		          	  alert("编号:"+rowData['shopOrderCode']+",退货量不能小于零");
		          	  $('#${name}').datagrid('beginEdit', index);
		          }else if(rowData.shopConfirmNum>rowData.chainConfirmNum){
		             alert("编号:"+rowData['shopOrderCode']+",退货量不能大于连锁审核量");
		          	  $('#${name}').datagrid('beginEdit', index);
		          }          
	          }
	          <!-- 配送收退货、交订货 -->
	          if(rowData.shopOrderType == 0){
		          if(rowData.logisticsSendNum<0){
		             alert("编号:"+rowData['shopOrderCode']+",实际配送量不能小于零");
		              $('#${name}').datagrid('beginEdit', index);
		          }else if(rowData.logisticsSendNum>rowData.logisticsAssignNum){
		             alert("编号:"+rowData['shopOrderCode']+",实际配送量不能大于分配量");
		             $('#${name}').datagrid('beginEdit', index);
		          }
	          }else{
		          if(rowData.logisticsReceiveNum<0){
		             alert("编号:"+rowData['shopOrderCode']+",实收量不能小于零");
		              $('#${name}').datagrid('beginEdit', index);
		          }else if(rowData.logisticsReceiveNum>rowData.chainConfirmNum){
		             alert("编号:"+rowData['shopOrderCode']+",实收量不能大于确认量");
		             $('#${name}').datagrid('beginEdit', index);
		          }          
	          }                   
          },
          onUnselect:function(index, rowData) {
<!--				_grid.datagrid('endEdit', index); -->
		  }
	});	
};
</script>

</#macro>